package LuaSystem;
/*
 * LuaEngine.java
 * Created on August 9, 2008, 12:44 PM by Uru
 *
 * A static class that contains Lua
 */

import org.keplerproject.luajava.*;

public class LuaEngine {
    protected static LuaState lua;
    
    //start up the lua engine
    public static void initializeLua(){
        //initialize Lua
        lua = LuaStateFactory.newLuaState();
        lua.openLibs();
        
        //register Java functions
        JavaPrintLine jprintln = new JavaPrintLine(lua);
        try{
            jprintln.register("println");
        }catch(LuaException e){
            System.err.println("Failed to register println() in Lua");
            System.err.println(e.toString());
        }
    }
    
    //return the lua engine, in case someone wants to do something unusual with it
    public static LuaState getLua(){
        return lua;
    }
    
    //--------------------------------------------------------------------------
    //Running Files and Strings
    //--------------------------------------------------------------------------
    
    //runs a lua file, returning 0 if successful and 1 if there was an error
    public static int runLuaFile(String file){
        return lua.LdoFile(file);
    }
    
    //runs a lua command string, returning 0 if successful and 1 if there was an error
    public static int runLuaString(String command){
        return lua.LdoString(command);
    }
    
    //--------------------------------------------------------------------------
    //Running a Lua function with no return value
    //--------------------------------------------------------------------------
    
    //calls the specified lua function with no arguments and or return value
    public static void executeLuaFunction(String command){
        lua.getGlobal(command);
        lua.call(0,0);
    }
    
    //calls the specified lua function with the given list of string arguments
    public static void executeLuaFunction(String command, String[] args){
        lua.getGlobal(command);
        for(int i=0; i< args.length; i++){
            lua.pushString(args[i]);
        }
        lua.call(args.length,0);
    }
    
    //calls the specified lua function with the given list of object arguments
    public static void executeLuaFunction(String command, Object[] args){
        lua.getGlobal(command);
        for(int i=0; i< args.length; i++){
            lua.pushJavaObject(args[i]);
        }
        lua.call(args.length,0);
    }
    
    //--------------------------------------------------------------------------
    //Running a Lua function with a string return value
    //--------------------------------------------------------------------------
    
    //calls the specified lua function with no arguments and returns a string from lua
    public static String executeLuaFunctionWithStringReturn(String command){
        lua.getGlobal(command);
        lua.call(0,1);
        return lua.toString(-1);
    }
    
    //calls the specified lua function with the given list of string arguments and returns a string from lua
    public static String executeLuaFunctionWithStringReturn(String command, String[] args){
        lua.getGlobal(command);
        for(int i=0; i< args.length; i++){
            lua.pushString(args[i]);
        }
        lua.call(args.length,1);
        return lua.toString(-1);
    }
    
    //calls the specified lua function with the given list of object arguments and returns a string from lua
    public static String executeLuaFunctionWithStringReturn(String command, Object[] args){
        lua.getGlobal(command);
        for(int i=0; i< args.length; i++){
            lua.pushJavaObject(args[i]);
        }
        lua.call(args.length,1);
        return lua.toString(-1);
    }
    
    //--------------------------------------------------------------------------
    //Get variable values from Lua by variable name
    //--------------------------------------------------------------------------
    
    public static String getGlobalString(String name){
        lua.getGlobal(name);
        String s = lua.toString(-1);
        lua.pop(1);
        return s;
    }
    
    //--------------------------------------------------------------------------
    //
    //--------------------------------------------------------------------------
}
